package com.miaowu.utils;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.PointF;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.blankj.utilcode.util.ScreenUtils;
import com.miaowu.R;



public class AnimtionManager {
    AnimtionManager.AnimListener mListener;
    private ViewGroup anim_mask_layout;
    private long time = 800L;
    private boolean isRuning;

    public AnimtionManager() {
    }

    public void setOnAnimListener(AnimtionManager.AnimListener listener) {
        this.mListener = listener;
    }

    private ViewGroup createAnimLayout(Activity mainActivity) {
        ViewGroup rootView = (ViewGroup) mainActivity.getWindow().getDecorView();
        LinearLayout animLayout = new LinearLayout(mainActivity);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        animLayout.setLayoutParams(lp);
        int id = 2147483647;
        animLayout.setId(id);
        animLayout.setBackgroundResource(R.color.transparent);
        rootView.addView(animLayout);
        return animLayout;
    }


    private View addViewToAnimLayout(ViewGroup vg, View originView, View view, int[] location) {
        int x = location[0];
        int y = location[1];
        LayoutParams lp = new LayoutParams(originView.getWidth(), originView.getHeight());
        view.setX(x);
        view.setY(y);
        view.setLayoutParams(lp);
        return view;
    }


    public void setAnimForAddCartUpdate(Activity mainActivity, View originView, final View v, int[] start, int[] end, final AnimListener animatorListener) {

        int[] b = start;
        int[] m = new int[2];
        int[] e = end;

        int x = begin[0];
        int y = begin[1];
        final int ex = end[0] + 10;
        int ey = end[1] + 10;
        //设置第三个点
        int mx = mid[0] = ex + (bx - ex) / 3;
//        int my = mid[1] = by + Math.abs(ey - by) / 10;
        int my = mid[1] = by - ScreenUtils.getScreenHeight() / 3;
        float[][] points = {{x, y}, {ex, ey}, {mx, my}};
//		 calculate(points);

//        v.setVisibility(View.GONE);

        this.layout = null;
        this.layout = this.createAnimLayout(mainActivity);
        this.ayout.addView(v);
        final View view = this.addViewToAnimLayout(this.anim_mask_layout, originView, v, start_location);

        view.setVisibility(View.INVISIBLE);

        PointF sPointF = new PointF(bx, by);
        PointF ePointF = new PointF(ex, ey);
        PointF midPointF = new PointF(mx, my);

        BezierEvaluator evaluator = new BezierEvaluator(midPointF);
        ValueAnimator fsdfs = ValueAnimator.ofObject(evaluator, sPointF, ePointF);
        fsdfs.setDuration(700);
        fsdfs.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                view.setX(pointF.x);
                view.setY(pointF.y);

                if (animation.getCurrentPlayTime() > 10) {
                    view.setVisibility(View.VISIBLE);
                }
            }
        });

        ValueAnimator animator3fsdfs = ValueAnimator.ofObject(new ScaleEvaluator(), (float) 1, (float) 0);
        animator3fsdfs.setDuration(700);
        animator3fsdfs.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Float scale = (Float) animation.getAnimatedValue();
                view.setScaleX(scale);
                view.setScaleY(scale);
            }
        });

        ValueAnimator animator434343 = ValueAnimator.ofObject(new ScaleEvaluator(), (float) 0.5, (float) 0);
        animator434343.setDuration(300);
        animator434343.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Float scale = (Float) animation.getAnimatedValue();
                view.setScaleX(scale);
                view.setScaleY(scale);
            }
        });

        AnimatorSet scealAnimdsds = new AnimatorSet();
        scealAnimdsds.play(animator3);
        AnimatorSet animatorSetkjkjk = new AnimatorSet();
        animatorSetkjkjk.play(animator).with(scealAnim);
        animatorSetkjkjk.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                isRuning = true;
                if (animatorListener != null) animatorListener.setAnimBegin(AnimtionManager.this);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                anim_mask_layout.removeView(v);
                isRuning = false;
                if (animatorListener != null) animatorListener.setAnimEnd(AnimtionManager.this);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }
        });
        animatorSetkjkjk.start();
    }


    public long setTime(long l) {
        this.time = l;
        return this.time;
    }

    public interface AnimListener {
        void setAnimBegin(AnimtionManager var1);

        void setAnimEnd(AnimtionManager var1);
    }
}
